package com.mofang.ticketing.common.core.util;


import cn.hutool.core.util.StrUtil;

import java.net.MalformedURLException;
import java.net.URL;

/**
 * @author LGC
 * @date 2021/7/30 10:00
 * @copyright 2021 mofang. All rights reserved
 */
public class FileUtil {

    /**
     * 拼接文件路径 aaa/bbb/ccc/
     *
     * @param fileNameParts
     * @return
     */
    public static String concat(final String... fileNameParts) {
        String res = "";
        for (final String part : fileNameParts) {
            if (!StrUtil.isEmpty(res)) {
                res = ensureEndsWith(res, "/");
            }
            res += part;
        }
        return res;
    }

    public static String ensureEndsWith(String str, final String suffix) {
        if (!StrUtil.endWith(str, suffix)) {
            str += suffix;
        }
        return str;
    }

    /**
     * get url相对路径
     *
     * @param urlStr
     * @return
     */
    public static String relativePath(String urlStr) {
        String relativePath = null;
        URL url = null;
        try {
            url = new URL(urlStr);
            relativePath = url.getPath().substring(1);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        return relativePath;
    }


}
